package com.chenfan.mcn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.ChooseExecutionDTO;
import com.chenfan.mcn.model.dto.PurchaseContractDTO;
import com.chenfan.mcn.model.dto.PurchaseContractSearchDTO;
import com.chenfan.mcn.model.vo.ChooseExecution;
import com.chenfan.mcn.model.vo.PurchaseContractVO;
import com.chenfan.mcn.model.vo.PurchaseSimpleVO;
import com.chenfan.mcn.model.vo.PurchaseVO;
import com.chenfan.mcn.service.IncomeContractService;
import com.chenfan.mcn.service.PurchaseContractService;
import com.chenfan.privilege.common.config.SearchAuthority;
import com.chenfan.privilege.common.config.UrlAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 采购合同
 * @Author liuyang
 * @Date 2021/2/26
 * @Time 18:48
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/purchase")
public class PurchaseContractController {

    @Autowired
    private PurchaseContractService purchaseContractService;

    @Autowired
    private IncomeContractService incomeContractService;

    /**
     * 新增采购合同
     *
     * @param purchaseContract {@link PurchaseContractDTO}
     * @return {@link Res}
     */
    @PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    @UrlAuth({"procurementContract_add","procurementContract_edit"})
    public Res<Long> save(@RequestBody @Validated PurchaseContractDTO purchaseContract) {
        return Res.ok(purchaseContractService.saveData(purchaseContract));
    }

    /**
     * 查询指定收入合同下关联的采购合同单号
     *
     * @param incomeContractCode {@link String}
     * @return {@link Res}
     */
    @GetMapping(value = "/getByContract", produces = {"application/json;charset=UTF-8"})
    public Res<List<PurchaseSimpleVO>> getPurchaseByContract(@RequestParam String incomeContractCode) {
        Assert.notNull(incomeContractCode, "收入合同编号不能为空");
        return Res.ok(purchaseContractService.getPurchaseByContract(incomeContractCode));
    }


    /**
     * 修改采购合同状态
     *
     * @param id     {@link Long}
     * @param status {@link Integer}
     * @return {@link Res}
     */
    @GetMapping(value = "/status/change")
    @UrlAuth({"procurementContract_revocation","procurementContract_cancellation","procurementContract_submit"})
    public Res<Object> changeStatus(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status)) {
            throw new BusinessException(McnErrorCode.PARAM_ERROR);
        }
        return purchaseContractService.changeStatus(id, status) ? Res.ok() : Res.failed();
    }


    /**
     * 详情
     *
     * @param page 分页
     * @param id   {@link Long}
     * @return {@link Res<PurchaseContractVO>}
     */
    @GetMapping(value = "/info")
    public Res<PurchaseContractVO> info(@ApiIgnore McnPage<PurchaseContractVO> page, Long id) {
        return Res.ok(purchaseContractService.info(page, id));
    }


    /**
     * 分页数据
     *
     * @param page                      {@link McnPage}
     * @param purchaseContractSearchDTO {@link PurchaseContractSearchDTO}
     * @return
     */
    @GetMapping(value = "/getList", produces = {"application/json;charset=UTF-8"})
    @UrlAuth({"procurementContract_search"})
    public Res<Page<PurchaseVO>> getList(@ApiIgnore McnPage<PurchaseVO> page, @SearchAuthority PurchaseContractSearchDTO purchaseContractSearchDTO) {
        return Res.ok(purchaseContractService.getList(page, purchaseContractSearchDTO));
    }


    /**
     * 采购合同列表导出
     *
     * @param purchaseContractSearchDTO {@link PurchaseContractSearchDTO}
     * @return {@link Res}
     */
    @GetMapping(value = "/exportExcel")
    @UrlAuth({"procurementContract_export"})
    public Res<Object> exportExcel(@SearchAuthority PurchaseContractSearchDTO purchaseContractSearchDTO, @ApiIgnore HttpServletResponse response) {
        try {
            purchaseContractService.exportExcel(purchaseContractSearchDTO, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Res.failed(McnErrorCode.FAILED);
        }
        return Res.ok();
    }

    /**
     * 根据客户id获取收入合同编号
     *
     * @param customerId {@link Long}
     * @return {@link List<String>}
     */
    @GetMapping(value = "/getIncomeCode")
    public Res<List<String>> getIncomeCode(Long customerId) {
        return Res.ok(incomeContractService.getIncomeCode(customerId));
    }


    /**
     * 采购合同关联执行单弹窗
     *
     * @param page         分页
     * @param executionDTO 入参
     * @return
     */
    @GetMapping(value = "/getExecutionOrders")
    public Res<Page<ChooseExecution>> getExecutionOrders(@ApiIgnore McnPage<ChooseExecution> page, ChooseExecutionDTO executionDTO) {
        return Res.ok(purchaseContractService.getExecutionOrders(page, executionDTO));
    }


    /**
     * 详情列表导出
     *
     * @param id       {@link Long}
     * @param response {@link HttpServletResponse}
     */
    @GetMapping(value = "/infoExport")
    public void infoExport(Long id, @ApiIgnore HttpServletResponse response) throws Exception {
        purchaseContractService.infoExport(id, response);
    }

}
